/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.UserBO;
import cn.hallele.ms.portalcenter.application.bo.UserQueryBO;
import cn.hallele.ms.portalcenter.domain.action.user.UserDomainService;
import cn.hallele.ms.portalcenter.domain.domain.UserDomain;
import cn.hallele.ms.portalcenter.domain.domain.UserQueryDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/6 13:13
 */
@Slf4j
@Service
public class UserBDDas extends ModelDas<UserDomainService, UserBO, UserDomain> {
    @Autowired
    private RoleBDDas roleBDDas;

    @Autowired
    private MenuBDDas menuBDDas;

    protected UserBDDas(UserDomainService option) {
        super(option);
    }

    /**
     * 获取用户头像
     *
     * @param username 用户名
     * @return 用户信息
     */
    public String getAvatar(String username) {
        return this.option.getAvatar(username);
    }

    /**
     * 获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    public UserBO queryUserByUsername(String username) {
        return revert(this.option.queryUserByUsername(username));
    }

    /**
     * 通过用户名查询完整的用户信息，包括角色、菜单
     *
     * @param username 用户名
     * @return 完整的用户信息
     */
    public UserBO queryUserFullInfo(String username) {
        return revert(this.option.queryUserFullInfo(username));
    }

    /**
     * 用户分页查询
     *
     * @param userQueryBO userQueryBO
     * @return UserDTO
     */
    public Page<UserBO> listUser(UserQueryBO userQueryBO) {
        return revertPage(this.option.listUser(convertQuery(userQueryBO)));
    }

    /**
     * 转换 UserQueryBO -> UserQueryDomain
     *
     * @param userQueryBO UserQueryBO
     * @return UserQueryBO
     */
    private UserQueryDomain convertQuery(UserQueryBO userQueryBO) {
        return ObjectUtil.map(userQueryBO, u -> UserQueryDomain.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .deptId(u.getDeptId())
                .username(u.getUsername())
                .phoneNumber(u.getPhoneNumber())
                .status(u.getStatus())
                .keywords(u.getKeywords())
                .pageNo(u.getPageNo())
                .pageSize(u.getPageSize())
                .startTime(u.getStartTime())
                .endTime(u.getEndTime())
                .build());
    }

    /**
     * 将T转成E
     *
     * @param userBO 被转换者
     * @return S 转换后者
     */
    @Override
    protected UserDomain convert(UserBO userBO) {
        return ObjectUtil.map(userBO, u -> UserDomain.builder()
                .tenantId(u.getTenantId())
                .id(u.getId())
                .username(u.getUsername())
                .password(u.getPassword())
                .avatar(u.getAvatar())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(u.getSex())
                .age(u.getAge())
                .status(u.getStatus())
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .pageNo(u.getPageNo())
                .pageSize(u.getPageSize())
                .keywords(u.getKeywords())
                .startTime(u.getStartTime())
                .endTime(u.getEndTime())
                .menus(menuBDDas.convert(u.getMenus()))
                .roles(roleBDDas.convert(u.getRoles()))
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param userDomain 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected UserBO revert(UserDomain userDomain) {
        return ObjectUtil.map(userDomain, u -> UserBO.builder()
                .tenantId(u.getTenantId())
                .id(u.getId())
                .username(u.getUsername())
                .password(u.getPassword())
                .avatar(u.getAvatar())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(u.getSex())
                .age(u.getAge())
                .status(u.getStatus())
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .pageNo(u.getPageNo())
                .pageSize(u.getPageSize())
                .keywords(u.getKeywords())
                .startTime(u.getStartTime())
                .endTime(u.getEndTime())
                .menus(menuBDDas.revert(u.getMenus()))
                .roles(roleBDDas.revert(u.getRoles()))
                .build());
    }
}
